package com.googlecode.util.demo;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class ConfigurationUtilDemo {
	// Tham khao:
	// http://commons.apache.org/configuration/userguide/user_guide.html
	public static void main(String[] args) {
		try {
			// Tu dong tim file properties tu current directory ma chua cac folder src, resource, lib, bin...
			// Tim den user home directory
			// Roi tim den classpath
			PropertiesConfiguration config = new PropertiesConfiguration("config.properties");
			// hanh dong nay chi xoa properties trong object config, khong lam anh huong den file
			config.clearProperty("java.home");
			if(!config.isEmpty()) {
				System.out.println(config.getString("java.home"));
				System.out.println(config.getString("catalina.home"));
				System.out.println(config.getString("domain"));
				System.out.println(config.getString("title"));
				System.out.println(config.getString("application.title"));
				List<Object> colorList = config.getList("colors");
				if(!colorList.isEmpty()) {
					for(Object obj : colorList) {
						if(obj instanceof String) {
							String color = (String)obj;
							System.out.println("color: " + color);
						}
					}
				}
			}
			// them moi mot properties hoac sua properties theo key
			config.setProperty("test", "zzz");
			// save thong tin trong config object vao file test.properties trong thu muc bin
			// thu muc bin cung la thu muc hien hanh ma chua complied code
			config.save("temp.properties");
			// Hanh dong nay chi xoa cac properties trong config object, khong lam thay doi file properties
			config.clear();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
}
